package com.ryg.slideview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;

public class MainActivity extends Activity implements OnItemClickListener {


    private CustomSwipeListView mListView;

    private List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();
    private View view;//最外部的view，点击它时结束重命名


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        view=findViewById(R.id.re_main);
        view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				for (BluetoothDevice device : devices) {//不能用foreach语句，这样不能拿到你在编辑谁
//					if(device.getBluetoothDeviceEditable()==1){
//						device.setBluetoothDeviceEditable(0);
//					}
//				}
				for(int i=0;i<devices.size();i++){
					BluetoothDevice device=null;
					if(devices.get(i).getBluetoothDeviceEditable()==1){
						device=devices.get(i);
						View view=mListView.getChildAt(i);
						EditText edit=(EditText) view.findViewById(R.id.edit_device_name);
						String name=edit.getText().toString();
                        device.setBluetoothDeviceEditable(0);
                        device.setBluetoothDeviceName(name);
                        //记得在网络或SQLite中更新
					}
				}
				adapter.notifyDataSetChanged();
			}
		});
    }

    private void initView() {
        mListView = (CustomSwipeListView) findViewById(R.id.list);
        adapter=new BluetoothDeviceAdapter(this,devices);
        
        BluetoothDevice item = null;
        for (int i = 0; i < 5; i++) {
        	item= new BluetoothDevice();
        	item.setBluetoothDeviceId(i);
            item.setBluetoothDeviceName("畅哥"+i);
            item.setBluetoothDeviceType(0);
            item.setBluetoothDeviceEditable(0);
                devices.add(item);
            }
            
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(this);
    }

  
    private BluetoothDeviceAdapter adapter;

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
	}

   
}
